package com.kylin.common.util;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.taglib.html.SelectTag;

import com.kylin.common.Constant;

public class DictionarySelectTag extends BodyTagSupport   {

	protected String match[];

	protected String name;

	protected String property;

	protected String saveBody;

	protected String multiple;

	protected String size;

	protected String value;

	protected String dictName;

	public DictionarySelectTag() {
		match = null;
		multiple = null;
		name = "mytag-select";
		property = null;
		saveBody = null;
		size = null;
		value = null;
	}

	public int doStartTag() throws JspException {
		System.out.println("---start doStartTag \n");
        try {
			pageContext.getOut().print(renderSelectStartElement());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return 2;
	}

	protected String renderSelectStartElement() throws JspException {
		StringBuffer results = new StringBuffer("<select");
		results.append(" name=\"");

		results.append(property);
		results.append("\"");

		if (multiple != null)
			results.append(" multiple=\"multiple\"");
		if (size != null) {
			results.append(" size=\"");
			results.append(size);
			results.append("\"");
		}

		results.append(">");
		return results.toString();
	}

	public int doEndTag() throws JspException {
		System.out.println("---start doEndTag \n");
		// pageContext.removeAttribute("org.apache.struts.taglib.html.SELECT");
		setOptions();
		StringBuffer results = new StringBuffer();
		if (saveBody != null)
			results.append(saveBody);
		results.append("</select>");
		try {
			pageContext.getOut().print(results.toString());
		} catch (IOException e) {
			throw new JspException(e);
		}
		return 6;
	}

	private void setOptions(){
	    StringBuffer sb = new StringBuffer();
		if(this.dictName == null){
			sb.append("<not found dict />");
			return;
		}
		if(Constant.dictionarys.get(dictName) instanceof SelectOption[]){
		    SelectOption[] options = (SelectOption[])Constant.dictionarys.get(dictName);
		    for(int i=0;i < options.length ; i++){
		    	sb.append("<option value=\"");
		    	sb.append(options[i].getValue());
		    	sb.append("\">");
		    	sb.append(options[i].getText());
		    	sb.append("</option>");
		    }
		    
		    saveBody = sb.toString();
		}
		 
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getSaveBody() {
		return saveBody;
	}

	public void setSaveBody(String saveBody) {
		this.saveBody = saveBody;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDictName() {
		return dictName;
	}

	public void setDictName(String dictName) {
		this.dictName = dictName;
	}
}
